import os
import pygame
import math
import time
from pygame.locals import *
import stery1
import vector
import seek
import flee
import arrive
import wander
import pathfollow

#------------------------------------------------------------------------------
# Name: AssaultMarine
# Desc: klasa czesciowo zerznieta z tutoriala - obsluga spriteow
#------------------------------------------------------------------------------

true = True
false = False

class AssaultMarine( pygame.sprite.Sprite ):
    
    
    
    steering = arrive.Arrive()
    steeringForce = vector.Vector(0.0,0.0)
    startTime = time.time()
    targetPos = vector.Vector(525.0, 325.0)
    path =[]
    path.append(targetPos)
    temp = vector.Vector(25.0, 425.0)
    path.append(temp)
    temp = vector.Vector(150.0, 125.0)
    path.append(temp)
    temp = vector.Vector(250.0, 225.0)
    path.append(temp)
    temp = vector.Vector(525.0, 25.0)
    path.append(temp)

    #USTAWIENIA USTAWIENIA USTAWIENIA USTAWIENIA
    selectable = True
    auto = False
    isFlee = False
    isSeek = False
    isPath = False
    isFollower=False
    isWander = False
    isArrival = False
    leader=0
    MAXSPEED = 3.0
    MAXFORCE = 20.0
    MASS = 102.5
    squad=0
    LUDEKPOSITION = vector.Vector(240, 110)
    stopDistance=600
    #bedzie uzywane do wykrywania kolizji...
    wykrywanie = 0
    #USTAWIENIA USTAWIENIA USTAWIENIA USTAWIENIA
    def setStopDistance(self,dystans):
        stopDistance =dystans    
    
    def setAsLeader(self):
        self.image = stery1.loadImageFile( "DA_Assualt_Sergeant_Energyfist.png", True )
        self.selectable = True
        self.auto = True
        self.isPath = True
    def setAsFollower(self):
        self.selectable = False
        self.auto = True
        self.isFollower = True
    
    
    def __init__( self, namea,leadera,squada ):
        # Intialize Sprite, our base class
        self.squad = squada
        self.name = namea
        pygame.sprite.Sprite.__init__( self )
        self.direction = 0.0
        self.lastdirection = 0.0
        self.speed=0.0
        
        self.image = stery1.loadImageFile( "DA_Assualt_Marine.png", True )
        self.rect = self.image.get_rect()
        self.rect.center = (self.LUDEKPOSITION.x,self.LUDEKPOSITION.y)
        
        surface = pygame.display.get_surface()
        self.area = surface.get_rect()
       
        self.x_velocity = 0.0
        self.y_velocity = -12.0
        self.leader=leadera
        
        if (self.isFlee == True): 
            self.steering = flee.Flee()
        elif (self.isWander == True):
            orientation = vector.Vector(1.0, 2.0)
            velocity = vector.Vector(self.x_velocity, self.y_velocity)
            position = vector.Vector(self.rect.center[0], self.rect.center[1])
            self.steering = wander.Wander(position, orientation, velocity)
        elif (self.isArrival == True):
            self.steering = arrive.Arrive()
        elif (self.isFollower == True):
            self.steering = seek.Arrive()
        else:
            self.steering = pathfollow.Pathfollow()
            self.steering.setPath(self.path)
            self.steering.orientation = vector.Vector(1.0, 2.0)
            self.steering.velocity = vector.Vector(self.x_velocity, self.y_velocity)             
            self.steering.position = vector.Vector(self.rect.center[0], self.rect.center[1])
        
        
        self.steering.max_speed = self.MAXSPEED 
        self.steering.max_force = self.MAXFORCE                                                
        self.steering.mass = self.MASS                                                  



    def update( self, neighbors ):
        
        # TRYB MANUALNY
        
        if (self.auto == False):
            keyinput = pygame.key.get_pressed()
            if self.lastdirection-self.direction>89.9 or self.lastdirection-self.direction<-89.9:
                temp = ((self.lastdirection - self.direction) - (self.lastdirection - self.direction)%90)
                self.image=pygame.transform.rotate(self.image,temp)            
                self.lastdirection = self.direction
                
            if keyinput[K_LEFT]:
                #self.direction += 5.0
                self.x_velocity-=0.5
                time.sleep(0.1)
            if keyinput[K_RIGHT]:
                #self.direction -= 5.0
                self.x_velocity+=0.5
                time.sleep(0.1)
            if keyinput[K_UP]:
                #self.speed -=0.2
                self.y_velocity-=0.5
            if keyinput[K_DOWN]:
                #self.speed +=0.2
                self.y_velocity+=0.5
            
            #self.x_velocity=math.sin((self.direction*math.pi)/180.0)*self.speed
            #self.y_velocity=math.cos((self.direction*math.pi)/180.0)*self.speed
        # TRYB AUTOMATYCZNY -> sterowaniem ruchu zajmuja sie sily sterujace
        else:
            
            self.steering.velocity = vector.Vector(self.x_velocity, self.y_velocity)               
            self.steering.position = vector.Vector(self.rect.center[0], self.rect.center[1])
            if (self.isWander == True):
                steeringForce = self.steering.calculate()
            elif (self.isPath == True):
                steeringForce = self.steering.calcPath(self)
            elif (self.isFollower == True):
                steeringForce = self.steering.calculate(vector.Vector(self.leader.rect.centerx, self.leader.rect.centery))    
                steeringForce = steeringForce.add( self.steering.separation(neighbors) )
                steeringForce = steeringForce.add( self.steering.aligment(neighbors) )
                steeringForce = steeringForce.add( self.steering.cohesion(neighbors) )    
            else:
                steeringForce = self.steering.calculate(self.targetPos)    
            
            if (steeringForce.length() != 0):
                self.steering.velocity = self.steering.velocity.add( (steeringForce.divide(self.steering.mass)).multiple(1.0) )   
                self.steering.velocity = self.steering.velocity.cutTo(self.steering.max_speed)
                
                self.x_velocity=self.steering.velocity.x
                self.y_velocity=self.steering.velocity.y
                
            else:
                self.x_velocity = 0
                self.y_velocity = 0
                
           
        
        
        # Now, move the sprite...
        self.rect.move_ip( (self.x_velocity, self.y_velocity ) )
        
        if self.rect.left < self.area.left:
            self.rect = self.rect.move(self.area.right,0.0)
        if self.rect.right > self.area.right:
            self.rect = self.rect.move(-self.area.right,0.0)  
        if self.rect.bottom > self.area.bottom:
            self.rect = self.rect.move(0.0,-self.area.bottom)  
        if self.rect.top < self.area.top:
            self.rect = self.rect.move(0.0,self.area.bottom)  
        

